import { addDays, format, isSameDay, isSameMonth, parseISO, startOfMonth, startOfWeek } from "date-fns";

export interface CalendarEvent {
  id: string;
  title: string;
  date: Date;
  time?: string;
  description?: string;
  color?: string;
}

export const SAMPLE_EVENTS: CalendarEvent[] = [
  {
    id: "1",
    title: "Team Meeting",
    date: parseISO("2025-05-10"),
    time: "10:00 AM",
    description: "Weekly team sync",
    color: "bg-blue-500",
  },
  {
    id: "2",
    title: "Project Deadline",
    date: parseISO("2025-05-15"),
    time: "5:00 PM",
    description: "Submit final deliverables",
    color: "bg-red-500",
  },
  {
    id: "3",
    title: "Lunch with Client",
    date: parseISO("2025-05-12"),
    time: "12:30 PM",
    description: "Discuss new requirements",
    color: "bg-green-500",
  },
  {
    id: "4",
    title: "Dentist Appointment",
    date: parseISO("2025-05-20"),
    time: "2:00 PM",
    description: "Regular checkup",
    color: "bg-purple-500",
  },
  {
    id: "5",
    title: "Birthday Party",
    date: parseISO("2025-05-25"),
    time: "7:00 PM",
    description: "Don't forget to bring a gift",
    color: "bg-yellow-500",
  },
];

export function getCalendarDays(date: Date): Date[] {
  const days: Date[] = [];
  const monthStart = startOfMonth(date);
  const weekStart = startOfWeek(monthStart);

  for (let i = 0; i < 42; i++) {
    days.push(addDays(weekStart, i));
  }

  return days;
}

export function getEventsForDay(events: CalendarEvent[], date: Date): CalendarEvent[] {
  return events.filter(event => isSameDay(event.date, date));
}

export function formatDateToMonthYear(date: Date): string {
  return format(date, "MMMM yyyy");
}

export function getDayClassName(day: Date, currentMonth: Date, selectedDate: Date | null): string {
  let className = "calendar-day ";

  if (isSameMonth(day, currentMonth)) {
    className += "calendar-day-current-month ";
  } else {
    className += "calendar-day-other-month ";
  }

  if (isSameDay(day, new Date())) {
    className += "calendar-day-today ";
  }

  if (selectedDate && isSameDay(day, selectedDate)) {
    className += "calendar-day-selected ";
  }

  return className;
}